package com.hcy.rabbitruntimeapi.center.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hcy.driver.DriverBaseInfoClient;
import com.hcy.driver.dto.request.DriverCarInfoRequestDTO;
import com.hcy.driver.dto.response.DriverCarInfoResponseDTO;
import com.hcy.rabbitbasicmodel.common.MyWebSocketMessage;
import com.hcy.rabbitbasicmodel.enums.Clients;
import com.hcy.rabbitbasicmodel.enums.Code;
import com.hcy.rabbitbasicmodel.enums.M_Code;
import com.hcy.rabbitbasicmodel.model.ResponseData;
import com.hcy.rabbitbasicutils.utils.RedisUtils;
import com.hcy.rabbitruntimeapi.center.MessageHandler;
import com.hcy.rabbitruntimeapi.center.MyMessageHandler;
import com.hcy.rabbitruntimeapi.handler.MyWebSocketFrameHandler;
import com.hcy.rabbitruntimeapi.model.dto.WebSocketKey;
import com.hcy.rabbitruntimeapi.model.message.driver.UploadTrackPointsDTO;
import com.hcy.schedulingclient.client.SchedulingLocationClient;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Point;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @Author huchenying
 * @Description
 * @Date 2021/12/15 19:28
 */
@MyMessageHandler(M_Code.CallCarLocation)
@Component
public class CallCarLocationHandler extends MessageHandler {

    Logger logger = LoggerFactory.getLogger(CallCarLocationHandler.class);

    @Autowired
    SchedulingLocationClient schedulingLocationClient;

    @Autowired
    RedisUtils redisUtils;

    @Autowired
    DriverBaseInfoClient driverBaseInfoClient;

    @Override
    public void handler(MyWebSocketMessage myWebSocketMessage) {
        logger.info("CallCarLocationHandler param - 》 [{}]",ToStringBuilder.reflectionToString(myWebSocketMessage));
        try {
            //验证参数 ， 调用运力系统
            JSONObject data = JSONObject.parseObject(JSON.toJSONString(myWebSocketMessage.getData()));
            String carId = data.getString("carId");
            String driverId = data.getString("driverId");
            String log = data.getString("log");
            String lat = data.getString("lat");
            MyWebSocketMessage oneMsg = new MyWebSocketMessage();
            MyWebSocketMessage twoMsg = new MyWebSocketMessage();
            Point car_geo_key = redisUtils.getCarLocationByCarId("CAR_GEO_KEY", carId);
            UploadTrackPointsDTO res = new UploadTrackPointsDTO();
            res.setLog(car_geo_key.getX());
            res.setLat(car_geo_key.getY());
            res.setTid(Integer.parseInt(carId));

            oneMsg.setCode(M_Code.CallCarLocation.getCode());
            oneMsg.setDesc(M_Code.CallCarLocation.getDesc());
            oneMsg.setData(JSON.toJSONString(res));
            //获取司机位置
            WebSocketKey webSocketKey = new WebSocketKey(myWebSocketMessage.getUsername(), Clients.CUSTOMER.getName());
            Channel channel = MyWebSocketFrameHandler.channels.get(webSocketKey);
            if(channel == null){
                throw new RuntimeException("channel is null!");
            }
            channel.writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(oneMsg)));
            //推送给司机自己位置
//            String log_lat = redisUtils.get("CUSTOMER_GEO_CACHE" + myWebSocketMessage.getUsername());
            //通过carId获取司机信息
//            DriverCarInfoRequestDTO driverCarInfoRequestDTO = new DriverCarInfoRequestDTO();
//            driverCarInfoRequestDTO.setCarId(Integer.parseInt(carId));
//            ResponseData<DriverCarInfoResponseDTO> responseData = driverBaseInfoClient.carDriverInfo(driverCarInfoRequestDTO);
//            if(responseData.getCode() != Code.Success){
//                throw new RuntimeException("司机信息获取失败");
//            }
//            String driverPhone = responseData.getData().getDriverPhone();
            WebSocketKey webSocketKey2 = new WebSocketKey(driverId, Clients.DRIVER.getName());
            Channel channel2 = MyWebSocketFrameHandler.channels.get(webSocketKey2);
            if(channel2 == null){
                throw new RuntimeException("channel2 is null!");
            }
            UploadTrackPointsDTO res2 = new UploadTrackPointsDTO();
            res2.setLog(Double.parseDouble(log));
            res2.setLat(Double.parseDouble(lat));
            res2.setTid(Integer.parseInt(carId));
            twoMsg.setCode(M_Code.GetCustomerLocation.getCode());
            twoMsg.setDesc(M_Code.GetCustomerLocation.getDesc());
            twoMsg.setData(JSON.toJSONString(res2));

            channel2.writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(twoMsg)));
        } catch (Exception e) {
            logger.error("处理失败！reason -> {}", ToStringBuilder.reflectionToString(e));
        }
    }
}
